/*
* H-ui.admin.js v2.4
* http://www.h-ui.net/
* Created & Modified by guojunhui
* Date modified 15:42 2016.03.14
*
* Copyright 2013-2016  All rights reserved.
* Licensed under MIT license.
* http://opensource.org/licenses/MIT
*
*/
var num = 0, oUl = $("#min_title_list"), hide_nav = $("#Hui-tabNav");

/*获取顶部选项卡总长度*/
function tabNavallwidth() {
    var taballwidth = 0,
		$tabNav = hide_nav.find(".acrossTab"),
		$tabNavWp = hide_nav.find(".Hui-tabNav-wp"),
		$tabNavitem = hide_nav.find(".acrossTab li"),
		$tabNavmore = hide_nav.find(".Hui-tabNav-more");
    if (!$tabNav[0]) { return }
    $tabNavitem.each(function (index, element) {
        taballwidth += Number(parseFloat($(this).width() + 60))
    });
    $tabNav.width(taballwidth + 25);
    var w = $tabNavWp.width();
    if (taballwidth + 25 > w) {
        $tabNavmore.show()
    }
    else {
        $tabNavmore.hide();
        $tabNav.css({ left: 0 })
    }
}

/*左侧菜单响应式*/
function Huiasidedisplay() {
    if ($(window).width() >= 768) {
        $(".Hui-aside").show()
    }
}
function getskincookie() {
    var v = getCookie("Huiskin");
    var hrefStr = $("#skin").attr("href");
    if (v == null || v == "") {
        v = "default";
    }
    if (hrefStr != undefined) {
        var hrefRes = hrefStr.substring(0, hrefStr.lastIndexOf('skin/')) + 'skin/' + v + '/skin.css';
        $("#skin").attr("href", hrefRes);
    }
}
//链接指向tab的封装方法
function Hui_admin_tab(obj) {
    if ($(obj).attr('_href')) {
        var bStop = false;
        var bStopIndex = 0;
        var _href = $(obj).attr('_href');
        var _titleName = $(obj).attr("data-title");//tab标题文字
        var _hrefType = $(obj).attr("_hrefType");//菜单打开方式
        if (_hrefType == "New") {
            window.open(_href);
        } else if (_hrefType == "Js") {
            eval(_hrefType);
        } else {
            var topWindow = $(window.parent.document);
            var show_navLi = topWindow.find("#min_title_list li");
            show_navLi.each(function () {
                if ($(this).find('span').attr("data-href") == _href) {
                    bStop = true;
                    bStopIndex = show_navLi.index($(this));
                    return false;
                }
            });
            if (!bStop) {
                creatIframe(_href, _titleName);
                min_titleList();
            }
            else {
                show_navLi.removeClass("active").eq(bStopIndex).addClass("active");
                var iframe_box = topWindow.find("#iframe_box");
                iframe_box.find(".show_iframe").hide().eq(bStopIndex).show().find("iframe").attr("src", _href);
            }
        }
    }
}
function min_titleList() {
    var topWindow = $(window.parent.document);
    var show_nav = topWindow.find("#min_title_list");
    var aLi = show_nav.find("li");
};
/*创建窗体*/
function creatIframe(href, titleName) {
    var topWindow = $(window.parent.document);
    var show_nav = topWindow.find('#min_title_list');
    show_nav.find('li').removeClass("active");
    var iframe_box = topWindow.find('#iframe_box');
    show_nav.append('<li class="active"><span data-href="' + href + '">' + titleName + '</span><i></i><em></em></li>');
    var taballwidth = 0,
		$tabNav = topWindow.find(".acrossTab"),
		$tabNavWp = topWindow.find(".Hui-tabNav-wp"),
		$tabNavitem = topWindow.find(".acrossTab li"),
		$tabNavmore = topWindow.find(".Hui-tabNav-more");
    if (!$tabNav[0]) { return }
    $tabNavitem.each(function (index, element) {
        taballwidth += Number(parseFloat($(this).width() + 60))
    });
    $tabNav.width(taballwidth + 25);
    var w = $tabNavWp.width();
    if (taballwidth + 25 > w) {
        $tabNavmore.show()
    }
    else {
        $tabNavmore.hide();
        $tabNav.css({ left: 0 })
    }
    var iframeBox = iframe_box.find('.show_iframe');
    iframeBox.hide();
    iframe_box.append('<div class="show_iframe"><div class="loading"></div><iframe frameborder="0" src=' + href + '></iframe></div>');
    var showBox = iframe_box.find('.show_iframe:visible');
    showBox.find('iframe').load(function () {
        showBox.find('.loading').hide();
    });

}
function removeIframe() {
    var topWindow = $(window.parent.document);
    var iframe = topWindow.find('#iframe_box .show_iframe');
    var tab = topWindow.find(".acrossTab li");
    var showTab = topWindow.find(".acrossTab li.active");
    var showBox = topWindow.find('.show_iframe:visible');
    var i = showTab.index();
    tab.eq(i - 1).addClass("active");
    iframe.eq(i - 1).show();
    tab.eq(i).remove();
    iframe.eq(i).remove();
}

$(function () {
    getskincookie();
    //layer.config({extend: 'extend/layer.ext.js'});
    Huiasidedisplay();
    var resizeID;
    $(window).resize(function () {
        clearTimeout(resizeID);
        resizeID = setTimeout(function () {
            Huiasidedisplay();
        }, 500);
    });
    $(".nav-toggle").click(function () {
        $(".Hui-aside").toggle(100);
        $(".Hui-aside").children().add('overflow', 'hidden');
    });
    $(".Hui-aside").on("click", ".menu_dropdown dd li a", function () {

        if ($(window).width() < 768) {
            $(".Hui-aside").slideToggle("fast");
        }
    });
    /*左侧菜单的显示方式 共封装了三种方式 1.默认全部关闭 点击收缩  2.默认打开第一个 点击收缩  3.默认关闭  在点击收缩*/
    $.Huifold(".menu_dropdown dl dt", ".menu_dropdown dl dd", "fast", 3, "click");
    /*选项卡导航 新增*/
    $(".Hui-aside").on("click", ".menu_dropdown a", function () {
        $(".liactives").removeClass();  //移除所有的样式
        $(this).addClass("liactives");  //添加新的样式
        Hui_admin_tab(this);
    });
    $(".Hui-aside").on("click", ".menu_dropdown span", function () {
        if ($(this).attr("_href")!="") {
            $(".selected").removeClass();  //移除所有的样式
            $(this).parent().addClass("selected");  //添加新的样式
            Hui_admin_tab(this);
        }
    });
    $(document).on("click", "#min_title_list li", function () {
        var bStopIndex = $(this).index();
        var iframe_box = $("#iframe_box");
        $("#min_title_list li").removeClass("active").eq(bStopIndex).addClass("active");
        iframe_box.find(".show_iframe").hide().eq(bStopIndex).show();
    });
    $(document).on("click", "#min_title_list li i", function () {
        var aCloseIndex = $(this).parents("li").index();
        $(this).parent().remove();
        $('#iframe_box').find('.show_iframe').eq(aCloseIndex).remove();
        num == 0 ? num = 0 : num--;
        tabNavallwidth();
    });
    $(document).on("dblclick", "#min_title_list li", function () {
        var aCloseIndex = $(this).index();
        var iframe_box = $("#iframe_box");
        if (aCloseIndex > 0) {
            $(this).remove();
            $('#iframe_box').find('.show_iframe').eq(aCloseIndex).remove();
            num == 0 ? num = 0 : num--;
            $("#min_title_list li").removeClass("active").eq(aCloseIndex - 1).addClass("active");
            iframe_box.find(".show_iframe").hide().eq(aCloseIndex - 1).show();
            tabNavallwidth();
        } else {
            return false;
        }
    });
    tabNavallwidth();

    $('#js-tabNav-next').click(function () {
        num == oUl.find('li').length - 1 ? num = oUl.find('li').length - 1 : num++;
        toNavPos();
    });
    $('#js-tabNav-prev').click(function () {
        num == 0 ? num = 0 : num--;
        toNavPos();
    });

    function toNavPos() {
        oUl.stop().animate({ 'left': -num * 100 }, 100);
    }


});
/*浮窗设置 */
var Msg_win = {
    set: function () {//最小化与恢复状态切换
        var set = this.minbtn.status == 1 ? [0, 1, 'block', this.char[0], '最小化'] : [1, 0, 'none', this.char[1], '展开'];
        this.minbtn.status = set[0];
        this.win.style.borderBottomWidth = set[1];
        this.content.style.display = set[2];
        this.minbtn.innerHTML = set[3]
        this.minbtn.title = set[4];
    },
    close: function () {//关闭
        this.win.style.display = 'none';
        window.onscroll = null;
        var st;
        window.onscroll = function () {
            clearTimeout(st);
            st = setTimeout(function () {
                //scrollMenu();
            }, 100);
        };
    },
    setOpacity: function (x) {//设置透明度
        var v = x >= 100 ? '' : 'Alpha(opacity=' + x + ')';
        this.win.style.visibility = x <= 0 ? 'hidden' : 'visible'; //IE有绝对或相对定位内容不随父透明度变化的bug
        this.win.style.filter = v;
        this.win.style.opacity = x / 100;
    }, //
    show: function () {//渐显
        clearInterval(this.timer2);
        var me = this, fx = this.fx(0, 100, 0.1), t = 0;
        this.timer2 = setInterval(function () {
            t = fx();
            me.setOpacity(t[0]);
            if (t[1] == 0) { clearInterval(me.timer2) }
        }, 10);
    }, //
    fx: function (a, b, c) {//缓冲计算
        var cMath = Math[(a - b) > 0 ? "floor" : "ceil"], c = c || 0.1;
        return function () { return [a += cMath((b - a) * c), a - b] }
    },
    init: function () {//创建HTML
        function $(id) { return document.getElementById(id) };
        this.win = $('msg_win');
        var set = { minbtn: 'msg_min', closebtn: 'msg_close', title: 'msg_title', content: 'msg_down' };
        for (var Id in set) { this[Id] = $(set[Id]) };
        var me = this;
        this.minbtn.onclick = function () { me.set(); this.blur() };
        this.closebtn.onclick = function () { me.close() };
        this.char = navigator.userAgent.toLowerCase().indexOf('firefox') + 1 ? ['_', '::', '×'] : ['0', '2', 'r']; //FF不支持webdings字体
        this.minbtn.innerHTML = this.char[0];
        this.closebtn.innerHTML = this.char[2];
        return this;
    }
};












